package router

import (
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"live-core/core"
	"live-core/logging"
	"net/http"
	"time"
)

func HTTPAPIServerDocumentation(c *gin.Context) {
	c.HTML(http.StatusOK, "documentation.tmpl", gin.H{
		"port":    core.StreamManager.ServerHTTPPort(),
		"streams": core.StreamManager.Streams,
		"version": time.Now().String(),
		"page":    "documentation",
	})
}

func HTTPAPIServerTodo(c *gin.Context) {
	c.HTML(http.StatusOK, "todo.tmpl", gin.H{
		"port":    core.StreamManager.ServerHTTPPort(),
		"streams": core.StreamManager.Streams,
		"version": time.Now().String(),
		"page":    "todo",
	})
}

func HTTPAPIStreamList(c *gin.Context) {
	c.HTML(http.StatusOK, "stream_list.tmpl", gin.H{
		"port":    core.StreamManager.ServerHTTPPort(),
		"streams": core.StreamManager.Streams,
		"version": time.Now().String(),
		"page":    "stream_list",
	})
}

func HTTPAPIPageLogin(c *gin.Context) {
	c.HTML(http.StatusOK, "login.tmpl", gin.H{
		"port":    core.StreamManager.ServerHTTPPort(),
		"streams": core.StreamManager.Streams,
		"version": time.Now().String(),
		"page":    "login",
	})
}

func HTTPAPIPlayHls(c *gin.Context) {
	c.HTML(http.StatusOK, "play_hls.tmpl", gin.H{
		"port":    core.StreamManager.ServerHTTPPort(),
		"streams": core.StreamManager.Streams,
		"version": time.Now().String(),
		"page":    "play_hls",
		"uuid":    c.Param("uuid"),
		"channel": c.Param("channel"),
	})
}

func HTTPAPIPlayMse(c *gin.Context) {
	c.HTML(http.StatusOK, "play_mse.tmpl", gin.H{
		"port":    core.StreamManager.ServerHTTPPort(),
		"streams": core.StreamManager.Streams,
		"version": time.Now().String(),
		"page":    "play_mse",
		"uuid":    c.Param("uuid"),
		"channel": c.Param("channel"),
	})
}

func HTTPAPIPlayWebrtc(c *gin.Context) {
	c.HTML(http.StatusOK, "play_webrtc.tmpl", gin.H{
		"port":    core.StreamManager.ServerHTTPPort(),
		"streams": core.StreamManager.Streams,
		"version": time.Now().String(),
		"page":    "play_webrtc",
		"uuid":    c.Param("uuid"),
		"channel": c.Param("channel"),
	})
}

func HTTPAPIAddStream(c *gin.Context) {
	c.HTML(http.StatusOK, "add_stream.tmpl", gin.H{
		"port":    core.StreamManager.ServerHTTPPort(),
		"streams": core.StreamManager.Streams,
		"version": time.Now().String(),
		"page":    "add_stream",
	})
}

func HTTPAPIEditStream(c *gin.Context) {
	c.HTML(http.StatusOK, "edit_stream.tmpl", gin.H{
		"port":    core.StreamManager.ServerHTTPPort(),
		"streams": core.StreamManager.Streams,
		"version": time.Now().String(),
		"page":    "edit_stream",
		"uuid":    c.Param("uuid"),
	})
}

func HTTPAPIMultiview(c *gin.Context) {
	c.HTML(http.StatusOK, "multiview.tmpl", gin.H{
		"port":    core.StreamManager.ServerHTTPPort(),
		"streams": core.StreamManager.Streams,
		"version": time.Now().String(),
		"page":    "multiview",
	})
}

func HTTPAPIFullScreenMultiView(c *gin.Context) {
	var createParams MultiViewOptions
	err := c.ShouldBindJSON(&createParams)
	if err != nil {
		logging.ZapLog.Error(err.Error(), zap.String("call", "BindJSON"))
	}
	logging.ZapLog.Debug("HTTPAPIFullScreenMultiView", zap.String("call", "Options"))
	c.HTML(http.StatusOK, "fullscreenmulti.tmpl", gin.H{
		"port":    core.StreamManager.ServerHTTPPort(),
		"streams": core.StreamManager.Streams,
		"version": time.Now().String(),
		"options": createParams,
		"page":    "fullscreenmulti",
		"query":   c.Request.URL.Query(),
	})
}

func HTTPAPIServerIndex(c *gin.Context) {
	c.HTML(http.StatusOK, "index.tmpl", gin.H{
		"port":    core.StreamManager.ServerHTTPPort(),
		"streams": core.StreamManager.Streams,
		"version": time.Now().String(),
		"page":    "index",
	})

}
